﻿using System;
using System.ComponentModel;
using System.Windows.Input;
using Microsoft.Win32;
using SimpleSpreadsheet.Controls;
using SimpleSpreadsheet.ViewModels;

namespace SimpleSpreadsheet.Views
{
    /// <summary>
    /// Interaction logic for WorkbookView.xaml
    /// </summary>
    public partial class WorkbookView : WorkBaseControl
    {
        public WorkbookView()
        {
            InitializeComponent();
            BackgroundOperationCompleted += WorkbookView_BackgroundOperationCompleted;
        }

        /// <summary>
        /// Handles the end of operation running in background
        /// </summary>
        /// <param name="e"></param>
        private void WorkbookView_BackgroundOperationCompleted(RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
                throw e.Error;

            var workbookViewModel = GetViewModel() as WorkbookViewModel;
            if (workbookViewModel != null)
            {
                workbookViewModel.ShellViewModel.DisplayName += String.Format(" - {0}",
                                                                              workbookViewModel.
                                                                                  CurrentWorksheetViewModel.FileName);
                var cell = FindVisualChild<CellControl>(ActiveItem);
                var textbox = FindVisualChild<CustomTextBox>(ActiveItem);
                if (cell != null && textbox != null)
                {
                    textbox.Text = workbookViewModel.CurrentWorksheetViewModel.CurrentCell.Text;
                    cell.Focus();
                }
            }
        }

        /// <summary>
        /// Displays a blank worksheet
        /// </summary>
        private void New()
        {
            var workbookViewModel = GetViewModel() as WorkbookViewModel;
            if (workbookViewModel != null)
            {
                InvokeOperation(workbookViewModel.NewDocument, null);
            }
        }

        /// <summary>
        /// Shows 'open file' dialog and loads document in background
        /// </summary>
        public void Open()
        {
            var dlg = new OpenFileDialog {DefaultExt = ".txt", Filter = "Text documents (.txt)|*.txt"};
            bool? result = dlg.ShowDialog();

            if (result == true)
            {
                var workbookViewModel = GetViewModel() as WorkbookViewModel;

                if (workbookViewModel != null)
                    InvokeOperation(workbookViewModel.LoadDocument, dlg.FileName);
            }
        }

        /// <summary>
        /// Saves the document
        /// </summary>
        public void Save()
        {
            var workbookViewModel = GetViewModel() as WorkbookViewModel;
            if (workbookViewModel != null)
            {
                workbookViewModel.SaveDocument();
            }
        }

        /// <summary>
        /// Handles Open application command
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommandBinding_ExecutedOpen(object sender, ExecutedRoutedEventArgs e)
        {
            Open();
        }

        /// <summary>
        /// Handles Save application command
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommandBinding_ExecutedSave(object sender, ExecutedRoutedEventArgs e)
        {
            Save();
        }

        /// <summary>
        /// Handles New application command
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommandBinding_ExecutedNew(object sender, ExecutedRoutedEventArgs e)
        {
            New();
        }
    }
}